package gui.sidebar;

import exceptions.RelationAlreadyInSetException;
import exceptions.RelationNotInSetException;
import gui.sidebar.database.DatabaseSpecPanel;
import gui.sidebar.relations.RelationContainerPanel;
import gui.sidebar.transactions.TransactionContainerPanel;

import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Jason Ye
 * Date: 02/02/2012
 * Time: 16:31
 */
public class SidebarContainerPanel extends JPanel {

    private RelationContainerPanel relationContainerPanel;
    private TransactionContainerPanel transactionContainerPanel;
    private DatabaseSpecPanel databaseSpecPanel;

    public SidebarContainerPanel(RelationContainerPanel relationContainerPanel, TransactionContainerPanel transactionContainerPanel, DatabaseSpecPanel databaseSpecPanel) {

        this.relationContainerPanel = relationContainerPanel;
        this.transactionContainerPanel = transactionContainerPanel;
        this.databaseSpecPanel = databaseSpecPanel;

        initialise();


    }

    private void initialise() {

        //init boxlayout for the sidebar
        BoxLayout sideBarLayout = new BoxLayout(this, BoxLayout.PAGE_AXIS);
        this.setLayout(sideBarLayout);

        //standard size for relation and transaction panels
        Dimension inputSize = new Dimension(300, 400);
        // init relation panel
        relationContainerPanel.setSize(inputSize);

        //init transaction panel
        transactionContainerPanel.setSize(inputSize);
        
        // db spec panel
        databaseSpecPanel.setSize(inputSize);

        // add relation and transaction panels to the screen
        this.add(databaseSpecPanel);
        this.add(relationContainerPanel);
        this.add(transactionContainerPanel);

    }

    public void addRelationSelectionListeners(ListSelectionListener listSelectionListener) {
        relationContainerPanel.addRelationSelectionListeners(listSelectionListener);
    }

    public void addRemoveRelationButtonListener(ActionListener listener) {
        relationContainerPanel.addRemoveButtonListener(listener);
    }

    public void removeRelation() {
        relationContainerPanel.removeSelectedRelation();

    }

    public void addAddRelationButtonListener(ActionListener addRelationButtonListener) {
        relationContainerPanel.addAddRelationButtonListener(addRelationButtonListener);
    }

    public void addRelation(String s) throws RelationAlreadyInSetException {
        relationContainerPanel.addRelation(s);
    }

    public void renameRelation(String s, String s1) throws RelationNotInSetException, RelationAlreadyInSetException {
        relationContainerPanel.renameRelation(s, s1);

    }

    public String getCurrentlySelectedRelation() {
        return relationContainerPanel.getCurrentlySelected();
    }

    public void addRenameRelationButtonListener(ActionListener renameRelationButtonListener) {
        relationContainerPanel.addRenameRelationButtonListener(renameRelationButtonListener);
    }

    public void setSelectedRelation(String s) {
        relationContainerPanel.setSelectedRelation(s);
    }

    public void addDBSpecButtonListener(ActionListener specDialog) {
        databaseSpecPanel.addSpecifyDBSpecButtonListener(specDialog);
    }

    public void addEvaluateButtonListener(ActionListener evaluateButtonListener) {
        transactionContainerPanel.addEvaluateButtonListener(evaluateButtonListener);
    }
}
